import { ref, computed, reactive } from 'vue'
import router from "@/router/index.js";
import { defineStore } from 'pinia'
import { setStorage, getStorage, rmStorage } from '@/utils/local'
import { getAddr } from '@/api/user';
export const useUserStore = defineStore('user', () => {
  //注册完毕后自动填充账号密码  
  const uname = ref(undefined)
  const pwd = ref(undefined)
  function registDone(a, b) {
    uname.value = a
    pwd.value = b
  }
  //用户登录后的信息
  const userInfo = ref(JSON.parse(getStorage('user')))
  function setUserInfo(obj) {
    userInfo.value = obj
    setStorage('user', JSON.stringify(obj))
  }
  function clearInfo() {
    userInfo.value = undefined
    rmStorage('user')
    router.push('/login')

  }

  //  收货地址
  const addrList = ref(undefined)
  async function getAddrList() {
    const res = await getAddr(userInfo.value.id)
    addrList.value = res.data
  }

  // 订单数量
  const orderNum = ref({
    unpay: 0,
    payed: 0
  })
  function setOrderNum(obj){
    orderNum.value = obj
  }

  return { uname, pwd, registDone, userInfo, setUserInfo, clearInfo,addrList,getAddrList,orderNum,setOrderNum }
})
